package vip.mtdp.usercenter.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import vip.mtdp.cloud.controller.AppBaseAdviceController;
import vip.mtdp.usercenter.dto.UserDTO;
import vip.mtdp.usercenter.service.UserRpcService;

/**
 * @author wangguoqing
 * @Description 用户控制中心
 * @date 2019/12/4 14:15
 */
@Slf4j
@RestController
@RequestMapping(value = "/")
public class UserController extends AppBaseAdviceController {

    @Reference
    private UserRpcService userRpcService;

    @RequestMapping(value = "/login/v1")
    public UserDTO login(@RequestBody UserDTO userDTO){
        log.info("login userDTO = {}",userDTO);
        UserDTO mUserDTO = userRpcService.login(userDTO.getUserName(),userDTO.getLoginPwd());
        if(mUserDTO != null){
            return mUserDTO;
        }
        return null;
    }

    @RequestMapping(value = "/register/v1")
    public UserDTO register(@RequestBody UserDTO userDTO){
        log.info("register userDTO = {}",userDTO);
        //只创建帐号,是否需要创建客户/商户由业务决定
//      PersonDTO personDTO = userDTO.getPersonDTO();
//		MerchantDTO merchantDTO = userDTO.getMerchantDTO();
//		if(personDTO == null && merchantDTO == null){
//			throw new AppRTException("param_is_not_null","person or merchant is not null");
//		}
//		//1.person/merchant
//		if(personDTO != null){
//			personDTO = personRpcService.createPerson(personDTO);
//			userDTO.setPersonDTO(personDTO);
//		}
//		if(merchantDTO != null){
//			merchantDTO = merchantRpcService.createMerchant(merchantDTO);
//			userDTO.setMerchantDTO(merchantDTO);
//		}
        UserDTO mUserDTO = userRpcService.createUser(userDTO);
        return mUserDTO;
    }

    @RequestMapping("/query/user/v1")
    public UserDTO queryUserInfo(@RequestHeader("X-Party_Id") Long partyId,@RequestHeader("X-User_Id") Long userId){
        log.info("query user info userId = {},partyId = {}",userId,partyId);
        UserDTO userDTO = userRpcService.getUserByUserId(userId);
        return userDTO;
    }

}
